#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include "ds_pool.h"

#if 0

int ds_pool_init(ds_pool *dsp, unsigned int item_size, unsigned int item_cnt, unsigned char alloc_type, unsigned char *buf)
{
	unsigned int i;
	unsigned char *ptr;

	if (item_size < sizeof(struct list_head) || item_cnt < 2)
	{
		return -1;
	}

	dsp->item_size = item_size;
	dsp->item_cnt = item_cnt;
	dsp->alloc_type = alloc_type;
	dsp->alloc_cnt = 0;
	INIT_LIST_HEAD(&dsp->head);

	if (alloc_type == DS_POOL_ALLOC_TYPE_EXT)
	{
		dsp->buf = buf;
		dsp->free_cnt = item_size;
	}
	else if (alloc_type == DS_POOL_ALLOC_TYPE_PRE)
	{
		i = item_size * item_cnt;
		dsp->buf = (unsigned char *)malloc(i);
		if (dsp->buf == NULL)
		{
			return -1;
		}
		dsp->free_cnt = item_size;
	} 
	else
	{
		dsp->buf = NULL;
		dsp->free_cnt = 0;
	}

	if (dsp->buf)
	{
		ptr = dsp->buf;
		for (i = 0; i < item_cnt; i++)
		{
			list_add_tail((struct list_head *)ptr, &dsp->head);
			ptr += item_size;
		}
	}

	return 0;
}

int ds_pool_uninit(ds_pool *dsp)
{
	struct list_head *pos, *n;

	if (dsp->alloc_type == DS_POOL_ALLOC_TYPE_PRE)
	{
		free(dsp->buf);
	}
	else if (dsp->alloc_type == DS_POOL_ALLOC_TYPE_DYN)
	{
		if (dsp->alloc_cnt != dsp->free_cnt)
		{
			return -1;
		}

		list_for_each_safe(pos, n, &dsp->head)
		{
			free(pos);
		}
	}

	return 0;
}

#endif